package com.example.democlient;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.democlient.model.Task;
import com.example.democlient.network.SocketService;
import com.example.democlient.network.serverpackets.SM_TASK_FINISH_REQUEST;

@SuppressLint("HandlerLeak")
public class TaskQueryActivity extends Activity implements OnClickListener{

	TextView taskFlagValue;
	TextView taskAreaValue;
	TextView taskAddressValue;
	TextView taskTelValue;
	TextView taskRemarkValue;
	TextView mapLocValue;
	Button finishButton;
	String fromValue=null;
	int taskId;
	private MyApplication app;
	
	Handler mainHandler = new Handler() {  
        @Override  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
	            case 1:  
//	    	    	Intent toIntent = new Intent();
//	    	  		toIntent.setClass(TaskQueryActivity.this,TaskActivity.class);
//	    	  		toIntent.putExtra("from", "1");
//	    	  		startActivity(toIntent);
	    			app.flag = 1;
	            	if(fromValue!=null){
		    			for(int i=0;i<app.roadDesignList.size();i++){
		    				Task temp = app.roadDesignList.get(i);
		    				if(temp.getId() == taskId){
		    					temp.setFinishFlag((byte)1);
		    				}
		    			}   
	            	}else{
		    			for(int i=0;i<app.roadDesignList.size();i++){
		    				Task temp = app.roadDesignList.get(i);
		    				if(temp.getId() == taskId){
		    					temp.setFinishFlag((byte)1);
		    				}
		    			}

		    			if(app.getHander(RoadDesignActivity.class)!=null){
		    	    		Message msgtest = new Message();
		    	    		msgtest.what=5;
		    	    		app.getHander(RoadDesignActivity.class).sendMessage(msgtest);
		    			}	            		
	            	}

	              finish();
	            break;  
	            default:  
	            
	            break;  
            }  
            super.handleMessage(msg);  
        }  
    }; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_query);

		taskFlagValue = (TextView)this.findViewById(R.id.taskFlagValue);
		taskAreaValue = (TextView)this.findViewById(R.id.taskAreaValue);
		taskAddressValue = (TextView)this.findViewById(R.id.taskAddressValue);
		taskTelValue = (TextView)this.findViewById(R.id.taskTelValue);
		taskRemarkValue = (TextView)this.findViewById(R.id.taskRemarkValue);
		mapLocValue = (TextView)this.findViewById(R.id.mapLocValue);
		
		Intent _intent = getIntent();
		String content = _intent.getStringExtra("content");
		String from = _intent.getStringExtra("from");
		if(from!=null){
			this.fromValue = _intent.getStringExtra("from");
		}
		String[] contentValue = content.split("#");
		
		taskId = Integer.valueOf(contentValue[0]);
		if(contentValue[1]=="1"){
			taskFlagValue.setText("收件");
		}else{
			taskFlagValue.setText("派件");
		}
		
		taskAreaValue.setText(contentValue[2]);
		taskAddressValue.setText(contentValue[3]);
		taskTelValue.setText(contentValue[4]);
		taskRemarkValue.setText(contentValue[5]);
		if(contentValue[6]==null || contentValue[6]==""){
			mapLocValue.setText("未定位");
		}else
		{
			mapLocValue.setText("已定位");
		}
		
		finishButton = (Button)this.findViewById(R.id.finishBtn);
		finishButton.setOnClickListener(this);
		
		app = (MyApplication)this.getApplication();
        app.addActivity(this);
        app.setHandler(this.getClass(),mainHandler);	
    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	if(item.getItemId()==R.id.menu_settings){
    		app.exit();
    	}
		return true;
    }

	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.finishBtn ){
			if(SocketService.getInstance().sendPacket(new SM_TASK_FINISH_REQUEST(taskId), app)){
			}
		}
	}
    
}
